//
//  SISSExcelView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/8/6.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSExcelView.h"


@interface SISSExcelView ()

/// leftTableView
@property (weak, nonatomic) SISSExcelLeftTableView *leftTableView;

@end
@implementation SISSExcelView

- (void)dealloc {
    [self.leftTableView removeObserver:self forKeyPath:Excel_leftTableViewContentOffset];
    [self.contentTableView removeObserver:self forKeyPath:Excel_contentTableViewContentOffset];
    [self.contentTableView removeObserver:self forKeyPath:Excel_collectionViewContentOffset];
    
    [self.leftTableView removeObserver:self forKeyPath:Excel_seletedPath];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.borderColor = [[UIColor colorWithRed:0xe5 / 255.0 green:0xe5 / 255.0 blue:0xe5 / 255.0 alpha:1.0] CGColor];
        self.layer.cornerRadius = 1.0f;
        self.layer.borderWidth = 1.0f;
        self.clipsToBounds = YES;
        
        self.leftWidth = SCREEN_WIDTH * 0.25;
        self.topHeight = 2;
        self.contentWidth = SCREEN_WIDTH * 0.25;
        self.contentHeight = 0;
        
        [self initUI];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:Excel_leftTableViewContentOffset]) {
        self.contentTableView.contentOffset = self.leftTableView.contentOffset;
    } else if ([keyPath isEqualToString:Excel_contentTableViewContentOffset]) {
        self.leftTableView.contentOffset = self.contentTableView.contentOffset;
    } else if ([keyPath isEqualToString:Excel_seletedPath]) {
        self.contentTableView.seletedPath = self.leftTableView.seletedPath;
    }
}

#pragma mark - UI
- (void)initUI {
    
    SISSExcelLeftTableView *leftTableView = [[SISSExcelLeftTableView alloc] init];
    leftTableView.showsVerticalScrollIndicator = NO;
    leftTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    leftTableView.excelView = self;
    leftTableView.clipsToBounds = YES;
    [self addSubview:leftTableView];
    [leftTableView addObserver:self forKeyPath:Excel_leftTableViewContentOffset options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [leftTableView addObserver:self forKeyPath:Excel_seletedPath options:NSKeyValueObservingOptionNew context:nil];
    self.leftTableView = leftTableView;
    
    SISSExcelContentTableView *contentTableView = [[SISSExcelContentTableView alloc] init];
    contentTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;    contentTableView.excelView = self;
    [self addSubview:contentTableView];
    [contentTableView addObserver:self forKeyPath:Excel_contentTableViewContentOffset options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [contentTableView addObserver:self forKeyPath:Excel_collectionViewContentOffset options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    self.contentTableView = contentTableView;
    
    WeakSelf
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(weakSelf);
        make.width.mas_equalTo(weakSelf.leftWidth);
    }];
    
    [self.contentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.leftTableView.mas_right);
        make.top.right.bottom.equalTo(weakSelf);
    }];
}


- (void)reloadData {
    
    [self.leftTableView reloadData];
    [self.contentTableView reloadData];
}

- (void)reloadLeftTableViewData {
    [self.leftTableView reloadData];
}
- (void)reloadContentTbaleViewData {
    [self.contentTableView reloadData];
}

@end
